/**
 * 
 */
package com.nh.action;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.nh.entity.NgVersion;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：VersionAction
 * 
 * 类描述： 版本管理
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-8 上午12:11:48
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-8 上午12:11:48
 * 
 * 修改备注：
 * 
 * @version
 */
public class VersionAction extends BaseAction {
	private NgVersion versionInfo;
	private Long optionId;

	/**
	 * 版本列表
	 * 
	 * @return
	 */
	public String versionList() {
		Criteria criteria = this.createCriteria(NgVersion.class);
		criteria.addOrder(Order.desc("publishTime"));
		criteria.addOrder(Order.desc("createTime"));
		this.queryByPage(criteria, "versionAction!versionList.action", 10);
		return "versionList";
	}

	/**
	 * 版本添加页面
	 * 
	 * @return
	 */
	public String versionAddPage() {
		List<NgVersion> list = this.getBaseService().findAll(NgVersion.class);
//		int v = 1;
//		for (NgVersion version : list) {
//			if (version.getVersion() > v) {
//				v = version.getVersion();
//			}
//		}

		versionInfo = new NgVersion();
		versionInfo.setLevel(0);
//		versionInfo.setVersion(v);
		versionInfo.setCustomType(0);
		versionInfo.setState(0);
		return "versionAddPage";
	}

	/**
	 * 版本保存页面
	 * 
	 * @return
	 */
	public String versionAddSave() {
		versionInfo.setCreateAdmin("Test");
		versionInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		if (versionInfo.getState() == 1) {
			versionInfo.setPublishTime(new Timestamp(System.currentTimeMillis()));
		}
		versionInfo.setDownloadCount(0);
		this.getBaseService().save(versionInfo);
		return versionList();
	}

	/**
	 * 发布
	 * 
	 * @return
	 */
	public String versionPublish() {
		List<NgVersion> list = this.getBaseService().findByProperty("state", 1, NgVersion.class);
		for (NgVersion version : list) {
			version.setState(0);
			this.getBaseService().saveOrUpdate(version, NgVersion.class);
		}
		NgVersion version = this.getBaseService().findById(optionId, NgVersion.class);
		version.setState(1);
		version.setPublishTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().saveOrUpdate(version, NgVersion.class);
		return versionList();
	}

	/**
	 * 停用
	 * 
	 * @return
	 */
	public String versionStop() {
		NgVersion version = this.getBaseService().findById(optionId, NgVersion.class);
		version.setState(0);
		this.getBaseService().saveOrUpdate(version, NgVersion.class);
		return versionList();

	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String versionRemove() {
		NgVersion version = this.getBaseService().findById(optionId, NgVersion.class);
		this.getBaseService().delete(version);
		return versionList();
	}

	/**
	 * 版本修改页面
	 * 
	 * @return
	 */
	public String versionAddModify() {

		return versionList();
	}

	/**
	 * 咨询连接设置页面
	 * 
	 * @return
	 */
	public String consultingConnectionPage() {

		return "consultingConnection";
	}

	/**
	 * @return the versionInfo
	 */
	public NgVersion getVersionInfo() {
		return versionInfo;
	}

	/**
	 * @param versionInfo
	 *            the versionInfo to set
	 */
	public void setVersionInfo(NgVersion versionInfo) {
		this.versionInfo = versionInfo;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

}
